package cn.edu.nuc.onlinestore.frame;

import java.awt.BorderLayout;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import cn.edu.nuc.onlinestore.Person.Admin;

import cn.edu.nuc.onlinestore.Person.ObjectStream;

import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import javax.swing.JPasswordField;
import javax.swing.JButton;
import javax.swing.ImageIcon;
import java.awt.event.ActionListener;
import java.io.File;
import java.awt.event.ActionEvent;

public class AdminLogin extends JFrame {

	private JPanel contentPane;
	private JTextField usernameField;
	private JPasswordField passwordField;
	private JFrame frame;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminLogin frame = new AdminLogin();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminLogin() {

		this.frame = this;
		setTitle("中北线在商场管理系统-管理员登录");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 461, 349);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);

		JLabel label = new JLabel("用户名:");
		label.setBounds(87, 146, 54, 15);
		contentPane.add(label);

		JLabel label_1 = new JLabel("密  码:");
		label_1.setBounds(87, 185, 54, 15);
		contentPane.add(label_1);

		usernameField = new JTextField();
		usernameField.setBounds(151, 146, 197, 21);
		contentPane.add(usernameField);
		usernameField.setColumns(10);

		passwordField = new JPasswordField();
		passwordField.setBounds(151, 185, 197, 21);
		contentPane.add(passwordField);

		JButton button = new JButton("登录系统");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {

				Admin admin = ObjectStream.read(Admin.class, "admin/" + usernameField.getText().trim() + ".txt");
                 //System.out.println(admin.getPassword());
				if (admin != null) {
					if (admin.getUsername().equals(usernameField.getText().trim())
						&& admin.getPassword().equals(passwordField.getText().trim())) {

						AdminStore store = new AdminStore(frame, admin.getUsername());
						frame.dispose();
						//登陆后，登录框设为空
						usernameField.setText("");
						passwordField.setText("");
				        
						// 令界面可见
						store.setVisible(true);
					} else {
						JOptionPane.showMessageDialog(null, "用户名或密码错误！");
					}
				} else {
					JOptionPane.showMessageDialog(null, "非法用户，无权访问！");
					usernameField.setText("");
					passwordField.setText("");
				}

			}
		});
		button.setBounds(197, 223, 93, 23);
		contentPane.add(button);

		JLabel lblNewLabel = new JLabel("");
		lblNewLabel.setIcon(new ImageIcon(AdminLogin.class.getResource("../image/admin.jpg")));
		lblNewLabel.setBounds(204, 54, 86, 82);
		contentPane.add(lblNewLabel);
	}
}
